/* entity */
import Field from '@model/entity/Field'
/* enum */
import { QualityInfoTypeEnum, QualityInfoStateEnum } from '@model/enum/QualityInfoEnum'
import { getProductQualityStatusText, getQualityInfoPeriodFieldText } from '@src/util/qualityInfoConfig'
import { ProductFieldNameMappingEnum } from '@model/enum/FieldMappingEnum'
/* util */
import { randomString } from 'pub-bbx-utils'
import DateFormatEnum from '@model/enum/DateFormatEnum'
import i18n from '@src/locales';


// TODO-bdz 表单涉及逻辑
// 质保信息 质保期字段
const QUALITY_INFO_PERIOD_FIELD: Field = {
  defaultValue: null,
  displayName: i18n.t('product.qualityField.qualityRuleInfoPeriod') as string,
  fieldName: ProductFieldNameMappingEnum.QualityRuleInfoPeriod,
  formType: 'select',
  guideProfessions: [],
  id: randomString(),
  isDelete: 0,
  isGuideData: false,
  isHidden: 0,
  isNull: 1,
  isSearch: 0,
  isSystem: 1,
  isVisible: true,
  orderId: 8,
  placeHolder: '',
  show: true,
  setting: {
    isMulti: false, 
    dataSource: [{
      text: getQualityInfoPeriodFieldText('Auto'),
      value: QualityInfoTypeEnum.Auto,
    }, {
      text: getQualityInfoPeriodFieldText('Manual'),
      value: QualityInfoTypeEnum.Manual
    }], 
    selectType: 2
  },
  tableName: 'product'
}

// 质保信息 质保期分割线字段
const QUALITY_INFO_SEPARATOR_FIELD: Field = {
  defaultValue: null,
  displayName: i18n.t('product.qualityField.qualityRuleInfoSeparator') as string,
  fieldName: ProductFieldNameMappingEnum.QualityRuleInfoSeparator,
  formType: 'separator',
  guideProfessions: [],
  id: randomString(),
  isDelete: 0,
  isGuideData: false,
  isHidden: 0,
  isNull: 1,
  isSearch: 1,
  isSystem: 0,
  isVisible: true,
  orderId: 6,
  placeHolder: '',
  show: true,
  setting: {},
  tableName: 'product',
}

// 质保信息 质保状态字段
const QUALITY_INFO_STATE_FIELD: Field = {
  defaultValue: null,
  displayName: i18n.t('product.qualityField.qualityInfoStatus') as string,
  exportAlias: 'qualityStatus',
  fieldName: ProductFieldNameMappingEnum.QualityInfoStatus,
  formType: 'select',
  guideProfessions: [],
  id: randomString(),
  isDelete: 0,
  isGuideData: false,
  isHidden: 0,
  isNull: 1,
  isSearch: 0,
  isSystem: 1,
  isVisible: true,
  orderId: 11,
  placeHolder: '',
  show: true,
  setting: {
    isMulti: false,
    dataSource: [{
      text: getProductQualityStatusText('In'),
      value: QualityInfoStateEnum.In,
    }, {
      text: getProductQualityStatusText('Out'),
      value: QualityInfoStateEnum.Out
    }],
    // ['保内', '保外']
  },
  tableName: 'product',
}

function getQualityInfoStartTimeField(isNull: number, field?: Field): Field {
  // 质保信息 质保开始日期字段
  return {
    defaultValue: null,
    displayName: i18n.t('product.qualityField.qualityInfoStartTime') as string,
    exportAlias: 'qualityStartTime',
    fieldName: ProductFieldNameMappingEnum.QualityInfoStartTime,
    formType: 'date',
    guideProfessions: [],
    id: randomString(),
    isDelete: 0,
    isGuideData: false,
    isHidden: 0,
    isNull,
    isSearch: 0,
    isSystem: 1,
    isVisible: true,
    orderId: 8,
    placeHolder: '',
    show: true,
    setting: field?.setting || {},
    tableName: 'product'
  }
}

function getQualityInfoEndTimeField(isNull: number, field?:Field): Field {
  // 质保信息 质保结束日期字段
  return {
    defaultValue: null,
    displayName: i18n.t('product.qualityField.qualityInfoEndTime') as string,
    exportAlias: 'qualityEndTime',
    fieldName: ProductFieldNameMappingEnum.QualityInfoEndTime,
    formType: 'date',
    guideProfessions: [],
    id: randomString(),
    isDelete: 0,
    isGuideData: false,
    isHidden: 0,
    isNull,
    isSearch: 0,
    isSystem: 1,
    isVisible: true,
    orderId: 8,
    placeHolder: '',
    show: true,
    setting: field?.setting || {},
    tableName: 'product'
  }
}

function getQualityStartTimeField(defaultField?: Field): Field {
  const field = getQualityInfoStartTimeField(1)
  Object.assign(field, {
    fieldName: 'qualityStartTime',
    setting: {
      dateType: DateFormatEnum.YMD_UP,
      ...(defaultField?.setting || {}),
    }
  })
  return field
}
function getQualityEndTimeField(defaultField?: Field): Field {
  const field = getQualityInfoEndTimeField(1)
  Object.assign(field, {
    fieldName: 'qualityEndTime',
    setting: {
      dateType: DateFormatEnum.YMD_UP,
      ...(defaultField?.setting || {}),
    }
  })
  return field
}

function getQualityStatusField(isNull: number): Field {
  // 质保信息 质保状态字段
  return {
    defaultValue: null,
    displayName: i18n.t('product.qualityField.qualityInfoStatus') as string,
    fieldName: ProductFieldNameMappingEnum.QualityInfoStatus,
    formType: 'select',
    guideProfessions: [],
    id: randomString(),
    isDelete: 0,
    isNull,
    isSearch: 0,
    isSystem: 1,
    orderId: 11,
    placeHolder: '',
    // TODO-bdz 没有value对应 值
    setting: { isMulti: false, dataSource: ['保内', '保外'] }
  };
}

export {
  QUALITY_INFO_PERIOD_FIELD,
  QUALITY_INFO_SEPARATOR_FIELD,
  QUALITY_INFO_STATE_FIELD,
  getQualityInfoStartTimeField,
  getQualityInfoEndTimeField,
  getQualityStatusField,
  getQualityStartTimeField,
  getQualityEndTimeField
}

export default {
  QUALITY_INFO_PERIOD_FIELD,
  QUALITY_INFO_SEPARATOR_FIELD,
  QUALITY_INFO_STATE_FIELD,
  getQualityInfoStartTimeField,
  getQualityInfoEndTimeField,
  getQualityStartTimeField,
  getQualityEndTimeField
}